home *** CD-ROM | disk | FTP | other *** search
- /* noway.c -
- /* replace OK and Cancel in buttons with Way/NoWay or Not */
- /* play appropriate sound if button clicked */
- /* sounds are marked as 'sysheap/purgeable', */
- /* snd 128-137 == "positive sounds" */
- /* snd 138-147 == "negative sounds" */
-
- /* © copyright 1992, Eric Slosser, all rights reserved */
-
- #include <SetupA4.h>
- #include <Sound.h>
- #include <Traps.h>
-
- typedef pascal short (*TrackType) (ControlHandle,Point,ProcPtr);
-
- long gOldNewControl;
- TrackType gOldTrackControl;
- long gOldSetCTitle;
-
- WindowPtr gLastWindow; /* for "noway/way" combinations */
- long gDirID;
- short gVRefNum;
- Str31 gName;
-
- Str15 kWay = "\pWay",
- kNoWay = "\pNo Way",
- kNot = "\pNot!",
- kOkay = "\pOK",
- kCancel = "\pCancel";
-
- /*------------------------------*/
- pascal ControlHandle myNewControl( WindowPtr wind, Rect bounds, Str255 title,
- Boolean visible, short val, short min, short max, short procID, long refCon);
- pascal void mySetCTitle( ControlHandle ctl, Str255 title );
- pascal short myTrackControl( ControlHandle ctl, Point p, ProcPtr action );
- int pstrcmp ( StringPtr s, StringPtr t );
-
- /*------------------------------*/
- void ChangeTitle( StringPtr *title, WindowPtr wind );
- void ChangeTitle( StringPtr *title, WindowPtr wind )
- {
- if ( !pstrcmp(*title,kOkay) ) {
- *title = kWay;
- gLastWindow = wind;
- }
- else if ( !pstrcmp(*title,kCancel) ) {
- *title = (gLastWindow == wind) ? kNoWay : kNot;
- }
- }
- /*------------------------------*/
- pascal ControlHandle myNewControl(
- WindowPtr wind,
- Rect bounds,
- Str255 title,
- Boolean visible,
- short val, short min, short max,
- short procID,
- long refCon)
- {
- SetUpA4();
- if ( procID==pushButProc )
- ChangeTitle( &title, wind );
- asm {
- move.l gOldNewControl,a0
- move.l (sp)+,a4 /* RestoreA4 */
- unlk a6
- jmp (a0)
- }
- } /* myNewControl */
- /*------------------------------*/
- pascal void mySetCTitle(
- ControlHandle ctl,
- Str255 title)
- {
- SetUpA4();
- ChangeTitle( &title, (**ctl).contrlOwner );
- asm {
- move.l gOldSetCTitle,a0
- move.l (sp)+,a4 /* RestoreA4 */
- unlk a6
- jmp (a0)
- }
- }
- /*------------------------------*/
- void PlaySound( Boolean postiveSound );
- void PlaySound( Boolean postiveSound )
- {
- short saved,id,resRef;
- Handle soundH;
-
- saved = CurResFile();
- resRef = HOpenResFile( gVRefNum, gDirID, gName, fsRdWrPerm);
- if ( resRef<0 )
- goto error;
-
- UseResFile(resRef);
- do {
- id = Random();
- if ( id<0 ) id = -id;
- id = id%10;
- id += postiveSound? 128 : 138;
- soundH = GetResource('snd ',id);
- }
- while (soundH==nil);
-
- SndPlay(nil,soundH,true);
-
- UseResFile(saved);
- error:
- ;
- }
- /*------------------------------*/
- pascal short myTrackControl(
- ControlHandle ctl,
- Point p,
- ProcPtr action )
- {
- short part;
- unsigned char *cTitle;
-
- SetUpA4();
- part = (*gOldTrackControl)( ctl,p,action ); /* can you say "tail patch"? */
- if ( part==inButton ) {
- cTitle = (**ctl).contrlTitle;
- if (!pstrcmp(cTitle,kNoWay) || !pstrcmp(cTitle,kNot) )
- PlaySound(false);
- else if (!pstrcmp(cTitle,kWay) )
- PlaySound(true);
- }
- RestoreA4();
- return(part);
- } /* myTrackControl */
- /*------------------------------*/
- void SaveResFileInfo(void);
- void SaveResFileInfo(void)
- {
- Handle h;
- short resRef;
- FCBPBRec pb;
-
- asm {
- RecoverHandle
- move.l a0,h
- }
-
- resRef = HomeResFile(h);
- DetachResource(h);
-
- pb.ioFCBIndx = 0;
- pb.ioFCBIndx = 0;
- pb.ioNamePtr = gName;
- pb.ioRefNum = resRef;
- PBGetFCBInfo(&pb,false);
-
- gVRefNum = pb.ioFCBVRefNum;
- gDirID = pb.ioFCBParID;
- }
- /*------------------------------*/
- int pstrcmp ( StringPtr s, StringPtr t )
- {
- register int i;
-
- if ( *s != *t )
- return ( *s - *t ); /* >0 if s is longer, <0 is t is longer */
-
- for ( i=*s; *s==*t && i>=0; s++,t++,i-- )
- ;
-
- if ( i<0 )
- return( 0 );
- else
- return ( *s - *t );
- }
- /*------------------------------*/
- main()
- {
- RememberA0();
- SetUpA4();
-
- SaveResFileInfo();
-
- gOldTrackControl = (TrackType) NGetTrapAddress(_TrackControl,ToolTrap);
- NSetTrapAddress( (long) myTrackControl,_TrackControl,ToolTrap);
-
- gOldNewControl = NGetTrapAddress( _NewControl,ToolTrap);
- NSetTrapAddress( (long) myNewControl,_NewControl,ToolTrap);
-
- gOldSetCTitle = NGetTrapAddress( _SetCTitle,ToolTrap);
- NSetTrapAddress( (long) mySetCTitle,_SetCTitle,ToolTrap);
-
- RestoreA4();
- }
- /*------------------------------*/
-